﻿/**
 * @file		ct_dd_pmc_tmr32.c
 * @brief		PMC 32bit-Reload Timer driver Component Test
 * @note		None
 * @attention	None
 * 
 * <B><I>Copyright 2016 Socionext Inc.</I></B>
 */

#include <stdlib.h>
#include <string.h>
#include "ct_dd_pmc_tmr32.h"
#include "dd_pmc_tmr32.h"
#include "pmc.h"
#include "ddim_user_custom.h"

/*----------------------------------------------------------------------*/
/* Definition															*/
/*----------------------------------------------------------------------*/
//#define CO_CT_DD_TMR32_USTIME		// Enable;get time(us). Disenable:get time(ms)

/*----------------------------------------------------------------------*/
/* Enumeration															*/
/*----------------------------------------------------------------------*/

/*----------------------------------------------------------------------*/
/* Global Data															*/
/*----------------------------------------------------------------------*/
static DDIM_USER_SYSTIM	gDd_timer32_tim1;
static DDIM_USER_SYSTIM	gDd_timer32_tim2;

#if 0
static ULONG gDD_Timer32_Spin_Lock __attribute__((section(".LOCK_SECTION"), aligned(64)));
#endif

/*----------------------------------------------------------------------*/
/* Function																*/
/*----------------------------------------------------------------------*/
static VOID  ct_pmc_timer32_cb( VOID );

/**
 * @brief  Command main function for timer32 test.
 * @param  int argc		:The number of parameters
 *		   char** argv	:The value of parameters
 * @return VOID
 */
VOID Ct_Dd_Pmc_TMR32_Main( int argc, char** argv )
{
	INT32				ret;
	DDIM_USER_ER		er;
	ULONG				usec;
	ULONG				num;
	T_DD_PMC_TMR32_CTRL	tmr32_ctrl = {{0}};

	/*------*/
	/* Ctrl */
	/*------*/
	if( strcmp(argv[1], "ctrl") == 0 ){
		tmr32_ctrl.csr.word = 0x00000008;
		
		// DIV
		if( strcmp(argv[2], "32") == 0 ){
			tmr32_ctrl.csr.bit.csl = 2;
		}
		else if( strcmp(argv[2], "8") == 0 ){
			tmr32_ctrl.csr.bit.csl = 1;
		}
		else if( strcmp(argv[2], "2") == 0 ){
			tmr32_ctrl.csr.bit.csl = 0;
		}
		else if( strcmp(argv[2], "ext") == 0 ){
			tmr32_ctrl.csr.bit.csl = 3;
		}
		else {
			tmr32_ctrl.csr.bit.csl = 0;
		}
		
		// MODE
		if( strcmp(argv[3], "one") == 0 ){
			tmr32_ctrl.csr.bit.reld = 0;
		}
		else if( strcmp(argv[3], "reload") == 0 ){
			tmr32_ctrl.csr.bit.reld = 1;
		}
		else {
			tmr32_ctrl.csr.bit.reld = 0;
		}
		
		tmr32_ctrl.rlr1 = (ULONG)atoi(argv[4]);
		tmr32_ctrl.rlr2 = (ULONG)atoi(argv[5]);
		tmr32_ctrl.soft_counter = (UCHAR)atoi(argv[6]);
		tmr32_ctrl.pcallback = ct_pmc_timer32_cb;

		Ddim_Print( ("Dd_PMC_TMR32_Ctrl Arg( T_DD_PMC_TMR32_CTRL )///\n") );
		Ddim_Print( ("csr.trg_fixed0 = %d\n", tmr32_ctrl.csr.bit.trg_fixed0) );		/* pgr0872 */
		Ddim_Print( ("csr.cnte       = %d\n", tmr32_ctrl.csr.bit.cnte) );			/* pgr0872 */
		Ddim_Print( ("csr.uf_fixed1  = %d\n", tmr32_ctrl.csr.bit.uf_fixed1) );		/* pgr0872 */
		Ddim_Print( ("csr.inte       = %d\n", tmr32_ctrl.csr.bit.inte) );			/* pgr0872 */
		Ddim_Print( ("csr.reld       = %d\n", tmr32_ctrl.csr.bit.reld) );
		Ddim_Print( ("csr.outl       = %d\n", tmr32_ctrl.csr.bit.outl) );			/* pgr0872 */
		Ddim_Print( ("csr.csl        = %d\n", tmr32_ctrl.csr.bit.csl) );
		Ddim_Print( ("csr.rels       = %d\n", tmr32_ctrl.csr.bit.rels) );			/* pgr0872 */
		Ddim_Print( ("rlr1           = %d\n", tmr32_ctrl.rlr1) );
		Ddim_Print( ("rlr2           = %d\n", tmr32_ctrl.rlr2) );
		Ddim_Print( ("soft_counter   = %d\n", tmr32_ctrl.soft_counter) );
		Ddim_Print( ("pcallback      = 0x%x\n", tmr32_ctrl.pcallback) );
		Ddim_Print( ("///////////////////////////////////////\n") );

		/* Ctrl */
		ret = Dd_PMC_TMR32_Ctrl( &tmr32_ctrl );
		if( ret == D_DDIM_OK ){
			Ddim_Print( ("Dd_PMC_TMR32_Ctrl OK\n") );
		}
		else {
			Ddim_Print( ("timer Dd_PMC_TMR32_Ctrl error=%d\n", ret) );
		}
		Ddim_Print(("Timer32 IO_PMC.TMR.TMCSR.word =[%lx].\n", IO_PMC.TMR.TMCSR.word));
	}
	/*-----------*/
	/* Calculate */
	/*-----------*/
	else if( strcmp(argv[1], "cal") == 0 ){
		usec = (ULONG)atoi(argv[2]);

		ret = Dd_PMC_TMR32_Calculate( usec, &tmr32_ctrl );
		if( ret == D_DDIM_OK ){
			Ddim_Print( ("Dd_TMR32_Calculate OK\n") );
			Ddim_Print( ("rels         = %d\n", tmr32_ctrl.csr.bit.rels) );
			Ddim_Print( ("csl          = %d\n", tmr32_ctrl.csr.bit.csl) );
			Ddim_Print( ("outl         = %d\n", tmr32_ctrl.csr.bit.outl) );
			Ddim_Print( ("reld         = %d\n", tmr32_ctrl.csr.bit.reld) );
			Ddim_Print( ("inte         = %d\n", tmr32_ctrl.csr.bit.inte) );
			Ddim_Print( ("uf_fixed1    = %d\n", tmr32_ctrl.csr.bit.uf_fixed1) );
			Ddim_Print( ("cnte         = %d\n", tmr32_ctrl.csr.bit.cnte) );
			Ddim_Print( ("trg_fixed0   = %d\n", tmr32_ctrl.csr.bit.trg_fixed0) );
			Ddim_Print( ("rlr1         = %d\n", tmr32_ctrl.rlr1) );
			Ddim_Print( ("rlr2         = %d\n", tmr32_ctrl.rlr2) );
			Ddim_Print( ("soft_counter = %d\n", tmr32_ctrl.soft_counter) );
			Ddim_Print( ("pcallback    = 0x%x\n", tmr32_ctrl.pcallback) );
		}
		else {
			Ddim_Print( ("Dd_PMC_TMR32_Calculate ERR=%d\n", ret) );
		}
	}
	/*----------------*/
	/* Start / Resume */
	/*----------------*/
	else if( strcmp(argv[1], "start") == 0 ){
		
		/* Debug: Time */
		er = DDIM_User_Get_Tim( &gDd_timer32_tim1 );
		
		/* Start */
		ret = Dd_PMC_TMR32_Start();
		Ddim_Print( ("Dd_PMC_TMR32_Start OK\n") );
		Ddim_Print( ("Timer32 IO_PMC.TMR.TMCSR.word =[%lx].\n", IO_PMC.TMR.TMCSR.word) );
	}
	/*------*/
	/* stop */
	/*------*/
	else if( strcmp(argv[1], "stop") == 0 ){  		// Timer32 stop

		/* Stop */
		ret = Dd_PMC_TMR32_Stop();
		Ddim_Print( ("Dd_PMC_TMR32_Stop OK\n") );
		Ddim_Print( ("Timer32 IO_PMC.TMR.TMCSR.word =[%lx].\n", IO_PMC.TMR.TMCSR.word) );
	}
	/*-------*/
	/* Pause */
	/*-------*/
	else if( strcmp(argv[1], "pause") == 0 ){

		/* Pause */
		ret = Dd_PMC_TMR32_Pause();
		Ddim_Print( ("Dd_PMC_TMR32_Pause OK\n") );
		Ddim_Print( ("Timer32 IO_PMC.TMR.TMCSR.word =[%lx].\n", IO_PMC.TMR.TMCSR.word) );
	}
	/*-----------------------------------*/
	/* Get TMR Register and Soft Counter */
	/*-----------------------------------*/
	else if( strcmp(argv[1], "get_counter") == 0 ){
		ULONG	hw_count ;
		ULONG	sw_count ;

		/* Get H/W & S/W Counter */
		ret = Dd_PMC_TMR32_Get_Counter( &hw_count, &sw_count );
		if( ret == D_DDIM_OK ){
			Ddim_Print( ("Dd_PMC_TMR32_Get_Counter OK\n") );
			Ddim_Print( ("H/W counter = 0x%x, S/W Counter=%d\n", hw_count, sw_count) );
		}
		else {
			Ddim_Print( ("Dd_PMC_TMR32_Get_Counter ERR=%d\n", ret) );
		}
	}
	/*------------------------*/
	/* Get Timer's Ctrl table */
	/*------------------------*/
	else if( strcmp(argv[1], "get_ctrl") == 0 ){

		/* Get H/W & S/W Counter */
		ret = Dd_PMC_TMR32_Get_Ctrl( &tmr32_ctrl ) ;
		if( ret == D_DDIM_OK ){
			Ddim_Print( ("Dd_PMC_TMR32_Get_Ctrl OK\n") );
			Ddim_Print( ("Timer32 IO_PMC.TMR.TMCSR.word =[%lx].\n", IO_PMC.TMR.TMCSR.word) );
			Ddim_Print( ("csr         =0x%x\n", tmr32_ctrl.csr.word) );
			Ddim_Print( ("rlr1        =%d\n", tmr32_ctrl.rlr1) );
			Ddim_Print( ("rlr2        =%d\n", tmr32_ctrl.rlr2) );
			Ddim_Print( ("soft_counter=%d\n", tmr32_ctrl.soft_counter) );
			Ddim_Print( ("pcallback   =0x%x\n", tmr32_ctrl.pcallback) );
		}
		else {
			Ddim_Print( ("Dd_PMC_TMR32_Get_Ctrl ERR=%d\n", ret) );
		}
	}
	/*------------------*/
	/* Set Soft Counter */
	/*------------------*/
	else if( strcmp(argv[1], "set_softcounter") == 0 ){
		/* ch number */
		num = atoi(argv[2]);

		/* Set S/W Counter */
		ret = Dd_PMC_TMR32_Set_Softcounter( num ) ;
		if( ret == D_DDIM_OK ){
			Ddim_Print( ("Dd_PMC_TMR32_Set_Softcounter OK\n") );
		}
		else {
			Ddim_Print( ("Dd_PMC_TMR32_Set_Softcounter ERR=%d\n", ret) );
		}
	}
	/*------------------*/
	/* Get Soft Counter */
	/*------------------*/
	else if( strcmp(argv[1], "get_softcounter") == 0 ){

		/* Get S/W Counter */
		num = Dd_PMC_TMR32_Get_Softcounter() ;
		Ddim_Print( ("Dd_PMC_TMR32_Get_Softcounter OK\n") );
		Ddim_Print( ("Soft counter=%d\n", num) );
	}
	/*-----------------*/
	/* Set Reload Flag */
	/*-----------------*/
	else if( strcmp(argv[1], "set_reload") == 0 ){
		/* number */
		num = atoi(argv[2]);

		/* Set Reload Flag */
		ret = Dd_PMC_TMR32_Set_Reload_Flg( num ) ;
		Ddim_Print( ("Dd_PMC_TMR32_Set_Reload_Flg OK\n") );
	}
	/*-----------------*/
	/* Get Reload Flag */
	/*-----------------*/
	else if( strcmp(argv[1], "get_reload") == 0 ){

		/* Get Reload Flag */
		num = Dd_PMC_TMR32_Get_Reload_Flg();
		Ddim_Print( ("Dd_PMC_TMR32_Get_Reload_Flg OK\n") );
		Ddim_Print( ("  Reload Flag=%d\n", num) );
	}
	/*---------------------*/
	/* Utility : Set_Timer */
	/*---------------------*/
	else if( strcmp(argv[1], "u_set") == 0 ){
		usec = (ULONG)atoi(argv[2]);
		
		ret = Dd_PMC_TMR32_SetTimer(usec, ct_pmc_timer32_cb);
		if (ret == 0) {
			Ddim_Print(("Dd_PMC_TMR32_SetTimer OK\n"));
		}
		else {
			Ddim_Print(("Dd_PMC_TMR32_SetTimer ERR. ret=0x%x\n", ret));
		}
	}
	/*------------------------*/
	/* Utility : Start Reload */
	/*------------------------*/
	else if( strcmp(argv[1], "u_start_reload") == 0 ){
		
		/* Debug: Time */
		er = DDIM_User_Get_Tim( &gDd_timer32_tim1 );
		if( er != D_DDIM_USER_E_OK ){
			Ddim_Print(("get_tim() er=%d\n",er));
		}
		
		ret = Dd_PMC_TMR32_StartReload();
		Ddim_Print(("Dd_PMC_TMR32_StartReload OK\n"));
	}
	/*--------------------------*/
	/* Utility : Start OneShot  */
	/*--------------------------*/
	else if( strcmp(argv[1], "u_start_one") == 0 ){
		
		/* Debug: Time */
		er = DDIM_User_Get_Tim( &gDd_timer32_tim1 );
		if( er != D_DDIM_USER_E_OK ){
			Ddim_Print(("get_tim() er=%d\n",er));
		}
		
		ret = Dd_PMC_TMR32_StartOneshot();
		Ddim_Print(("Dd_PMC_TMR32_StartOneshot OK\n"));
	}
	/*---------------*/
	/* Set Out Level */
	/*---------------*/
	else if( strcmp(argv[1], "set_outlevel") == 0 ){

		ret = Dd_PMC_TMR32_Set_Outlevel(atoi(argv[2]));
		if (ret == 0) {
			Ddim_Print( ("Dd_PMC_TMR32_Set_Outlevel OK\n") );
			Ddim_Print( ("Timer32 IO_PMC.TMR.TMCSR.word =[%lx].\n", IO_PMC.TMR.TMCSR.word) );
		}
		else {
			Ddim_Print( ("Dd_PMC_TMR32_Set_Outlevel ERR. ret=0x%x\n", ret) );
		}
	}
	else{
		Ddim_Print( ("please check 2nd parameter!!\n" ) );
	}
}


/*-------------*/
/* 32bit Timer */
/*-------------*/
static VOID ct_pmc_timer32_cb( VOID )
{
	DDIM_USER_ER er = DDIM_User_Get_Tim( &gDd_timer32_tim2 );
	if( er != D_DDIM_USER_E_OK ){
		Ddim_Print(("get_tim() er=%d\n",er));
	}
	
	Ddim_Print(("TMR32 INT. %d\n", (ULONG)(gDd_timer32_tim2 - gDd_timer32_tim1) ));
	gDd_timer32_tim1 = gDd_timer32_tim2 ;
}
